package com.monitor.app_monitor.service.quartz.task;

import com.monitor.app_monitor.entity.TaskLogAnnotation;
import com.monitor.app_monitor.service.quartz.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName CellectDataCheck.java
 * @Author ning
 * @Description TODO
 * @CreateTime 2021年12月16日 10:18:00
 */

@DisallowConcurrentExecution //作业不并发
@Component
@Slf4j
public class CellectDataCheck implements Job {

    @Resource
    @Qualifier("cellectMap")
    private Map<String, AtomicInteger> cellectMap;

    @Resource
    private ExecuteScript executeScript;


    @Value("${dataPath}")
    private String dataPath;

    @Value("${script}")
    private String script;

    @Value("${fileinfo}")
    private  String fileInfoScript;

    @Value("${isupdata}")
    private String isUpdataScript;

    @TaskLogAnnotation
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        /**
         * 一干文件是否采集完成监控
         */

        final JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String emsName = (String) jobDataMap.get("emsName");
        String resType = (String) jobDataMap.get("resType");


        int resCode = 0;
        final ScriptExecResult scriptExecResult;

        if (emsName.equals("ZTE-U31-ZZ-DWDM") && resType.equals("CTP")) {
            try {
                IsUpdataScriptExecResult isUpdataScriptExecResult = executeScript.IsUpdataScript(isUpdataScript, "172.17.8.61", "20204");
                if (isUpdataScriptExecResult.getStatus().equals("err")) {
                    resCode = 1;
                } else {
                    scriptExecResult = executeScript.executeScript(script, emsName, resType, dataPath);
                    String  scriptResult = scriptExecResult.getStatus();
                    resCode = Integer.valueOf(scriptResult);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (emsName.equals("Huawei-NCE-SDH") && resType.equals("CTP")){
            String dataFormat = Dataformat.getDataFormat("yyyyMMdd");
            //这个网管CTP在/data10/YG/FTP/TRANS这个路径下  需要单独监控
            //File folder = new File("/Users/admin/logs");
            //log.info("特殊目录"+"/Users/admin/logs");
            File folder = new File("/data10/YG/FTP/TRANS/"+emsName+"/"+dataFormat+"/CTP");
            log.info("特殊目录："+"/data10/YG/FTP/TRANS/"+emsName+"/"+dataFormat+"/CTP");
            File []CTPList = folder.listFiles();

            log.info("CTPList.length = " + CTPList.length);

            for (File file : CTPList){
                if (file.isFile()){
                    //当前规则是如果找到一个文件就认为采集正常
                    resCode = 1;
                    log.info(file.getName());
                    log.info("Huawei-NCE-SDH 网管CTP 采集正常。");
                    break;
                }else {
                    resCode = 0;
                    log.info("Huawei-NCE-SDH 网管CTP 采集异常。");
                }
            }

        } else if (emsName.equals("Huawei-NCE-WDM") && resType.equals("CTP")){
            //这个网管CTP在/data10/YG/FTP/TRANS这个路径下  需要单独监控
            String dataFormat = Dataformat.getDataFormat("yyyyMMdd");
            //这个网管CTP在/data10/YG/FTP/TRANS这个路径下  需要单独监控
            File folder = new File("/data10/YG/FTP/TRANS/"+emsName+"/"+dataFormat+"/CTP");
            log.info("特殊目录"+"/data10/YG/FTP/TRANS/"+emsName+"/"+dataFormat+"/CTP");
            File []CTPList = folder.listFiles();

            for (File file : CTPList){
                if (file.isFile()){
                    //当前规则是如果找到一个文件就认为采集正常
                    resCode = 1;
                    log.info("Huawei-NCE-WDM 网管CTP 采集正常。");
                    break;
                }else {
                    resCode = 0;
                    log.info("Huawei-NCE-WDM 网管CTP 采集异常。");
                }
            }
        } else {
            try {
                scriptExecResult = executeScript.executeScript(script, emsName, resType, dataPath);
                log.info("一干参数：{},{},{},{}",script, emsName, resType, dataPath);
                String  scriptResult = scriptExecResult.getStatus();
                resCode = Integer.valueOf(scriptResult);

            } catch (Exception e) {
                log.info(String.valueOf(e));
            }
        }


        cellectMap.get(emsName+resType).set(resCode);
    }
}
